package com.example.shoppingmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.shoppingmallspringboot.entity.Comment;
import com.example.shoppingmallspringboot.entity.Goods;
import com.example.shoppingmallspringboot.entity.Orders;
import com.example.shoppingmallspringboot.mapper.CommentMapper;
import com.example.shoppingmallspringboot.mapper.GoodsMapper;
import com.example.shoppingmallspringboot.mapper.OrdersMapper;
import com.example.shoppingmallspringboot.service.CommentService;
import com.example.shoppingmallspringboot.util.FwResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private OrdersMapper ordersMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public FwResult getGoodsComment(Integer goodsId) {
        List<Map<String, Object>> commentList = commentMapper.getComment(goodsId);
        if (commentList.size() > 0) {
            return FwResult.ok(commentList);
        } else {
            return FwResult.okMsg("商品暂无评论");
        }
    }

    @Override
    public FwResult UserComment(List<Comment> commentList) {
        if (commentList.size() > 0) {
            for (Comment comment : commentList) {
                commentMapper.insert(comment);
                Integer orderId = comment.getOrderId();
                UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", orderId)
                        .set("is_comment", 1);
                ordersMapper.update(null, updateWrapper);
            }
            return FwResult.ok("评论成功");
        }
        return FwResult.ok("评论失败");
    }

    @Override
    public FwResult getUserComments(Integer userId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getUserId, userId)
                .orderByDesc(Comment::getCreateTime);
        
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        
        if (comments != null && !comments.isEmpty()) {
            List<Map<String, Object>> result = comments.stream().map(comment -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", comment.getCommentId());
                map.put("goods_id", comment.getGoodsId());
                map.put("content", comment.getContent());
                map.put("score", comment.getScore());
                map.put("create_time", comment.getCreateTime());
                
                Goods goods = goodsMapper.selectById(comment.getGoodsId());
                if (goods != null) {
                    map.put("goods_name", goods.getGoodsName());
                    map.put("goods_img", goods.getGoodsImage());
                }
                
                return map;
            }).collect(Collectors.toList());
            
            return FwResult.ok(result);
        } else {
            return FwResult.okMsg("暂无评论记录");
        }
    }

    @Override
    public FwResult deleteComment(Integer commentId) {
        int result = commentMapper.deleteById(commentId);
        if (result > 0) {
            return FwResult.ok("删除成功");
        }
        return FwResult.ok("删除失败");
    }
} 